package com.y.fund.aacore.restful;

import java.lang.reflect.Modifier;

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.y.fund.handler.BeanFactoryProxy;

@Component
public class RestResultExtractAsync {

    @Async
    public void beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
                                  ServerHttpResponse response) {
    	RestfulAdvice advice = returnType.getMethodAnnotation(RestfulAdvice.class);
        if(advice != null){
        	Class<? extends RestfulAdviceProcessor> [] processorClazzs = advice.postHandle();
        	if(null==processorClazzs || processorClazzs.length<1) {
        	}else {
        		for(Class<? extends RestfulAdviceProcessor> pr : processorClazzs) {
                	if ((pr == null) || Modifier.isAbstract(pr.getModifiers())) {
                	}else {
                		try {
                			RestfulAdviceProcessor processor = BeanFactoryProxy.getBeanByClazz(pr);
                			processor.beforeBodyWrite(body, returnType, selectedContentType, selectedConverterType, request, response);
                		}catch(Exception ex) {
                			ex.printStackTrace();
                		}
                	}
        		}
            }
        }
    } 
}
